package com.stone.framework.message;

import io.swagger.annotations.ApiModelProperty;

/**
 * 响应报文
 * 
 * @author whocares
 * @date 2020/03/28
 */
public class ResMessage<T> extends AbstractMessage<T> {

    private static final long serialVersionUID = -1782898266365905702L;

    @ApiModelProperty(value = "报文请求头", required = true)
    private Header header;

    /**
     * use for json
     */
    @Deprecated
    public ResMessage() {}

    protected ResMessage(T body, Header header) {
        super(body, null);
        this.header = header;
    }

    public static ResMessage<Void> success() {
        return build(null, Header.SUCCESS);
    }

    public static <T> ResMessage<T> success(T body) {
        return build(body, Header.SUCCESS);
    }

    public static ResMessage<Void> fail(String code, String desc) {
        return build(null, Header.build(code, desc));
    }

    public static ResMessage<Void> fail(String desc) {
        return build(null, Header.build("9999", desc));
    }

    public static <T> ResMessage<T> build(T body, Header header) {
        ResMessage<T> message = new ResMessage<>(body, header);
        return message;
    }

    public static <T> boolean successed(ResMessage<T> resMessage) {
        return Header.SUCCESS.equals(resMessage.header);
    }

    public boolean successed() {
        return Header.SUCCESS.equals(this.header);
    }

    public static <T> boolean failed(ResMessage<T> resMessage) {
        return !successed(resMessage);
    }

    public Header getHeader() {
        return header;
    }

    @Override
    public String toString() {
        return "ResMessage [header=" + header + ", " + super.toString() + "]";
    }

    public static class Header {

        public static final Header SUCCESS = new Header("0000", "success");

        public static final Header ERROR = new Header("9999", "error");

        @ApiModelProperty(value = "报文返回码", required = true)
        private String code;

        @ApiModelProperty(value = "报文返回描述", required = true)
        private String desc;

        private Header(String code, String desc) {
            this.code = code;
            this.desc = desc;
        }

        public String getCode() {
            return code;
        }

        public String getDesc() {
            return desc;
        }

        private static Header build(String code, String desc) {
            return new Header(code, desc);
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((code == null) ? 0 : code.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Header other = (Header)obj;
            if (code == null) {
                if (other.code != null)
                    return false;
            } else if (!code.equals(other.code))
                return false;
            return true;
        }

        @Override
        public String toString() {
            return "Header [code=" + code + ", desc=" + desc + "]";
        }

    }

}
